{extend name="layout" /}
{block name="content"}
<div class="row">
  <div class="col-xs-3">
    <form>
      <div class="input-group input-group-sm demoTable">
        <input type="text" class="form-control input-sm" name="keywords" id="demoReload" autocomplete="off">
        <!-- keywords -->
        <span class="input-group-btn">
          <button class="btn btn-info btn-flat layui-btn">搜 索</button>
        </span>
      </div>
    </form>
  </div>
  <div class="col-xs-9 demoTable" style="text-align: right">
    <button type="button" class="btn btn-success btn-sm cocoModal" isForm url="{:url('add')}" title="新增项目" fixat="1"><i
        class="fa fa-plus"></i> 新增</button>
  </div>
</div>
<table class="layui-table"
  lay-data="{height: 'full-75', cellMinWidth: 60, page: true, id:'lists',limit:30, url:'{:url('data_show_where',['whereData'=>$whereData])}'}"
  lay-filter="lists">
  <thead>
    <tr class="layui-colla-content">
      <th lay-data="{type:'checkbox'}">ID</th>
      <th lay-data="{field:'id', sort: true, width:60}">ID</th>
      <th lay-data="{field:'name'}">优惠券名称</th>
      <th lay-data="{field:'typename'}">优惠券分类</th>
      <th lay-data="{field:'disname'}">折扣类别</th>
      <th lay-data="{field:'price'}">价格(折扣)</th>
      <!-- <th lay-data="{field:'isgivetext'}">是否可转赠</th> -->
      <th lay-data="{field:'isbuyviptext'}">购买会员赠送</th>
      <!-- <th lay-data="{field:'validdays'}">有效天数</th> -->
      <th lay-data="{field:'nums'}">领取数量</th>
      <th lay-data="{field:'usenums'}">核销数量</th>
      <!-- <th lay-data="{field:'desc'}">描述</th>
      <th lay-data="{field:'tip'}">使用说明</th> -->
      <th lay-data="{field:'startdate'}">生效时间</th>
      <th lay-data="{field:'enddate'}">失效时间</th>
      <th lay-data="{field:'createrid'}">发行者</th>
      <th lay-data="{field:'auditstatus', toolbar: '#switchaudit'}">审核状态</th>
      <th lay-data="{field:'status', toolbar: '#switchstatus'}">状态</th>
      <th lay-data="{fixed: 'right', width:340, align:'center', toolbar: '#barDemo'}">操作</th>
  </thead>
</table>
<script type="text/html" id="switchaudit">
  <input type="checkbox" id= "{{d.id}}" name="auditstatus" value="{{d.auditstatus}}" lay-skin="switch" lay-text="已审核|待审核" lay-filter="setauditstatus" {{d.auditstatus == '已审核' ? 'checked' : ''}}>
</script>
<script type="text/html" id="switchstatus">
  <input type="checkbox" id= "{{d.id}}" name="status" value="{{d.status}}" lay-skin="switch" lay-text="显示|隐藏" lay-filter="setstatus" {{d.status == 1 ? 'checked' : ''}}>
</script>
<script type="text/html" id="barDemo">
  <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
  {in name="$uinfo.roleid" value="1,4"}
  <a class="layui-btn layui-btn-xs" lay-event="avaiable">经销商选择</a>
  {/in}
  <a class="layui-btn layui-btn-xs" lay-event="checkdata">旧会员补发</a>
</script>
{/block}
{block name="script"}
<script type="text/javascript">
  function checkdata(data) {
    console.dir(data);
    layer.confirm('您确定要补发吗？', function (index) {
      $.post(APP_URL + "/admin/coupons/sendcoupons?items=" + data.id, function (datajson) {
        layer.msg("操作成功", function () {
          layer.close(index);
        });
      }, 'json');
    });
  }
</script>
<script type="text/javascript">
  function audit(data) {
    console.dir(data);
    // layer.confirm('是否要通过审核', function (index) {
    //       $.post(APP_URL + "/admin/coupons/audit?items=" + data.id, function (datajson) {
    //         layer.msg("操作成功", function () {
    //           layer.close(index);
    //         });
    //       }, 'json');
    //     });
    layer.confirm('是否要通过审核？', {
      btn: ['通过', '驳回', '取消']
      , btn3: function (index, layero) {
        // 取消
      }
    }, function (index, layero) {
      // 通过
      $.post(APP_URL + "/admin/coupons/audit?items=" + data.id, function (datajson) {
        layer.msg("操作成功", function () {
          layer.close(index);
        });
      }, 'json');
    }, function (index) {
      // 驳回
      $.post(APP_URL + "/admin/coupons/rejectaudit?items=" + data.id, function (datajson) {
        layer.msg("操作成功", function () {
          layer.close(index);
        });
      }, 'json');
    });
  }
</script>
<script type="text/javascript">
  function avaiable(data) {
    console.dir(data);
    if (data['createrid'] != 'admin' && data['createrid'] != 'quyuadmin') {
      layer.open({
        title: '提示'
        , content: '经销商发行的优惠券不可再选择经销商'
      });
      return;
    }
    var layindex = layer.open({
      type: 2,
      area: ['800px', '600px'],
      fixed: false, //不固定
      maxmin: true,
      content: APP_URL + "/admin/coupons/choose_dealer?couponsid=" + data.id,
      title: '选择不可用经销商',
      btn: ['确定', '取消'],
      shade: [0.8, '#000000'],
      yes: function (index, layero) {
        //parent.$('#parentIframe').text('我被改变了');
        var iframeWin = window[layero.find('iframe')[0]['name']];
        formobj = iframeWin.$("form");
        ajaxUrl = formobj.attr("action");
        if (ajaxUrl == undefined || ajaxUrl == '') {
          ajaxUrl = defaultSaveUrl;
        }
        datajson = formobj.serialize();
        searchMap.postData = datajson;
        $.post(ajaxUrl, searchMap, function (datajson) {
          var serializeObj = {};
          if (datajson.status == 1) {
            $(formobj.serializeArray()).each(function () {
              serializeObj[this.name] = this.value;
            });
            layer.msg("操作成功", function () {
              layer.closeAll();
            });
            setTimeout(function () {
              location.reload();
            }, 300);
          }
          else {
            layer.msg(datajson.msg);
            return false;
          }
        }, 'json');
      }
    });
  }
</script>
<script>
  layui.use('table', function () {
    var table = layui.table
      , form = layui.form;
    //监听通过审核/未审核操作
    form.on('switch(setauditstatus)', function (obj) {
      value = obj.elem.checked === true ? 1 : 2;
      var datajson = {id:this.id, field:this.name, value:value};
      $.post("{:url('setauditstatus')}",datajson,function(datajson){
        var serializeObj={};
        if(datajson.status != 1){
          layer.msg(datajson.msg);
          return false;
        }
      },'json');
    });
    //监听显示/隐藏操作
    form.on('switch(setstatus)', function (obj) {
      value = obj.elem.checked === true ? 1 : 0;
      var datajson = {id:this.id, field:this.name, value:value};
      $.post("{:url('setstatus')}",datajson,function(datajson){
        var serializeObj={};
        if(datajson.status != 1){
          layer.msg(datajson.msg);
          return false;
        }
      },'json');
    });
  });
</script>
{/block}